package com.chenjun.core.service.sys.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.chenjun.common.bean.Pagination;
import com.chenjun.common.model.DataGridModel;
import com.chenjun.core.dao.generator.entity.SysArea;
import com.chenjun.core.dao.generator.mapper.SysAreaMapper;
import com.chenjun.core.dao.module.SysAreaRelationMapper;
import com.chenjun.core.service.sys.SysAreaService;
import com.chenjun.core.vo.sys.AreaTree;

/**
 * 
 * @Description: 区域业务实现
 * @author yanjinyin
 * @date 2017年7月29日 上午11:30:35
 */
@SuppressWarnings("serial")
@Service
@Transactional
public class SysAreaServiceImpl implements SysAreaService{

	private static final String SYS_AREA_MAPPER = "SysAreaMapper";
    private static final Logger logger = Logger.getLogger(SysAreaServiceImpl.class);

    @Autowired
    private SysAreaMapper sysAreaMapper;
    
    
    @Autowired
    private SysAreaRelationMapper sysAreaRelationMapper;

	public SysArea get(String id){
		return sysAreaMapper.selectByPrimaryKey(id);
	}

	public int create(SysArea record){
		return sysAreaMapper.insert(record);
	}

	public int delete(String id){
		return sysAreaMapper.deleteByPrimaryKey(id);
	}

	public int update(SysArea record){
		return sysAreaMapper.updateByPrimaryKey(record);
	}

	public int createOrUpdate(SysArea record){
		int rows = 0;
		if (StringUtils.isBlank(record.getId())) {
			rows = sysAreaMapper.insert(record);
		} else {
			rows = sysAreaMapper.updateByPrimaryKeySelective(record);
		}
		return rows;
	}

	public int doCountByCondition(DataGridModel param){
		return sysAreaRelationMapper.countByCondition(param);
	}

	public List<SysArea> doListByCondition(DataGridModel param){
		return sysAreaRelationMapper.selectByCondition(param);
	}
	
	public List<AreaTree> selectTreeByCondition(DataGridModel param){
		return sysAreaRelationMapper.selectTreeByCondition(param);
	}
	public Pagination doPagination(DataGridModel param){
		Pagination vo = new Pagination();
		List<SysArea> list = sysAreaRelationMapper.selectByCondition(param);
		param.setStart(0);
		param.setRows(0);
		int size = sysAreaRelationMapper.countByCondition(param);
		vo.setAaData(list);
		vo.setiTotalRecords(size);
		vo.setsEcho(param.getPage());
		vo.setiTotalDisplayRecords(size);
		return vo;
	}

	public SysArea getTypeById(String id) {
		SysArea dto = new SysArea();
		SysArea resources = sysAreaMapper.selectByPrimaryKey(id);
		if (resources != null) {
			BeanUtils.copyProperties(resources, dto);
		}
		return dto;
	}

	public List<Map> getArea(String content) {
		List<Map> result=new ArrayList<Map>(1);
		/**
		 * 根据区域名字模糊查询
		 */
		DataGridModel params=new DataGridModel();
		params.setRows(0);
		params.setStart(0);
		params.getParams().put("level", "0");
		params.getParams().put("vague_nameContent", "%"+content+"%");
		List<SysArea> u_list=sysAreaRelationMapper.selectByCondition(params);
		/**
		 * 取出区域列表中的ID和名字
		 */
		for(SysArea u : u_list){
			Map<String,Object> uni = new HashMap<>();
			uni.put("id", u.getId());
			uni.put("name", u.getName());
			result.add(uni);
		}
		if(CollectionUtils.isEmpty(result)||result.get(0)==null){
			Map m=new HashMap();
			m.put("name", "无该地区");
			m.put("id", "");
			result.add(m);
		}
		return result;
	}

	public SysArea getResource(String id) {
		SysArea resources = sysAreaMapper.selectByPrimaryKey(id);
		return resources;
	}
}